package team.epos.merchant.servlet;

import org.apache.commons.lang3.StringUtils;
import team.epos.merchant.dto.ResponseBody;
import team.epos.merchant.service.DishesService;
import team.epos.merchant.service.DishesServiceImpl;
import team.epos.utils.annotation.RequestMethod;
import team.epos.utils.pojo.Dishes;
import team.epos.utils.servlet.BaseServlet;
import team.epos.utils.util.JsonUtil;
import team.epos.utils.util.PojoParamUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

/**
 * @author CHENKI
 */
public class DishesManagerServlet extends BaseServlet {

    DishesService dishesService = new DishesServiceImpl();

    private int pageCount = 3;

    @RequestMethod(method = "findAllDishes")
    public void findAllDishes(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String sid = req.getParameter("sId");
        req.setAttribute("sId", sid);
        req.getRequestDispatcher("/html/dishesManagerment.html").forward(req, resp);
    }

    @RequestMethod(method = "pagecount")
    public void getPageCount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String sid = req.getParameter("sId");
        List resListNoPage = dishesService.findAllDishes(Integer.parseInt(sid));
        int count = 0;
        count = resListNoPage.size() / pageCount;
        if (resListNoPage.size() % pageCount != 0) {
            count++;
        }
        resp.getWriter().write(JsonUtil.obJectToJson(new ResponseBody(count)));
    }

    @RequestMethod(method = "dishesdata")
    public void getDishesData(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String sid = req.getParameter("sId");
        String pageIndex = req.getParameter("pageIndex");
        List resList = dishesService.findAllDishesWithPage(Integer.parseInt(sid), Integer.parseInt(pageIndex), pageCount);
        resp.getWriter().write(JsonUtil.obJectToJson(resList));
    }

    @RequestMethod(method = "loadDisheCount")
    public void loadDisheCount(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Dishes dishes = PojoParamUtil.getObject(req, Dishes.class);
        List resList = null;
        resList = dishesService.loadDishes(dishes.getName(), dishes.getsId());
        int count = 0;
        count = resList.size() / pageCount;
        if (resList.size() % pageCount != 0) {
            count++;
        }
        resp.getWriter().write(JsonUtil.obJectToJson(new ResponseBody(count)));
    }

    @RequestMethod(method = "loadDisheWithPage")
    public void loadDisheWithPage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageIndex = req.getParameter("pageIndex");
        Dishes dishes = PojoParamUtil.getObject(req, Dishes.class);
        List resList = dishesService.loadDishesWithPage(dishes.getName(), dishes.getsId(), Integer.parseInt(pageIndex), pageCount);
        resp.getWriter().write(JsonUtil.obJectToJson(resList));
    }

    @RequestMethod(method = "loadDishe")
    public void loadDishe(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Dishes dishes = PojoParamUtil.getObject(req, Dishes.class);
        List resList = dishesService.loadDishes(dishes.getName(), dishes.getsId());
        resp.getWriter().write(JsonUtil.obJectToJson(resList));
    }

    @RequestMethod(method = "addDishes")
    public void addDishe(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int i = 0;
        Dishes dishes = PojoParamUtil.getObject(req, Dishes.class);
        if (StringUtils.isBlank(dishes.getImg())) {
            dishes.setImg("default");
        }
        i = dishesService.addDishes(dishes);
        if (i > 0) {
            resp.getWriter().write(JsonUtil.obJectToJson(new ResponseBody("success", i)));
        } else {
            resp.getWriter().write(JsonUtil.obJectToJson(new ResponseBody("failed", i)));
        }
    }

    @RequestMethod(method = "deletedishe")
    public void deleteDishe(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Dishes dishes = PojoParamUtil.getObject(req, Dishes.class);
        int i = 0;
        i = dishesService.deleteDishes(dishes.getId());
        if (i > 0) {
            resp.getWriter().write(JsonUtil.obJectToJson(new ResponseBody("success")));
        } else {
            resp.getWriter().write(JsonUtil.obJectToJson(new ResponseBody("faild")));
        }
    }

    @RequestMethod(method = "updateDishe")
    public void updateDishes(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        int i = 0;
        Dishes dishes = PojoParamUtil.getObject(req, Dishes.class);
        i = dishesService.upodateDishe(dishes);
        if (i > 0) {
            resp.getWriter().write(JsonUtil.obJectToJson(new ResponseBody("success")));
        } else {
            resp.getWriter().write(JsonUtil.obJectToJson(new ResponseBody("faild")));
        }
    }
}
